<template>
  <div>
    <el-dialog
      :close-on-click-modal="false"
      v-model="visibleDialog"
      title="领用凭证号选择"
      style="min-width: 1132px"
      draggable
      align-center
      @close="closeDialog"
      destroy-on-close
      class="selectDialog"
    >
      <!-- <div class="search-container"> -->
      <el-form :model="searchParam" :inline="true" label-width="70px">
        <el-form-item label="产品" prop="currentProductId">
          <el-select
            v-model="searchParam.currentProductId"
            placeholder="请选择产品"
            clearable
          >
            <el-option
              v-for="item in productList"
              :key="`${item.id}`"
              :label="`${item.productCode}-${item.productName}`"
              :value="`${item.id}`"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="领用人">
          <SelectPerson
            :maxCollapseTags="5"
            :isMultiple="false"
            :width="`100%`"
            placeholder="请选择领用人"
            :showSelectPerson="selectPersonData"
            @get-person="getPerson"
          />
        </el-form-item>
        <el-form-item label="申请日期">
          <el-date-picker
            v-model="searchParam.applyDateSearch"
            type="datetimerange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            value-format="YYYY-MM-DD HH:mm:ss"
            @change="getapplyDate"
          />
        </el-form-item>
        <el-form-item label="领用类型">
          <el-select
            v-model="searchParam.applyType"
            placeholder="请选择领用类型"
            clearable
          >
            <el-option label="领用申请" :value="1" />
            <el-option label="换型领用申请" :value="2" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="search"
            ><i-ep-search />搜索</el-button
          >
          <el-button @click="resetHandle"><i-ep-refresh />重置</el-button>
        </el-form-item>
      </el-form>
      <!-- </div> -->
      <!-- <el-card shadow="never" class="table-container"> -->
      <div class="formTable">
        <el-table
          ref="singleTableRef"
          :data="tableData"
          style="width: 100%"
          highlight-current-row
          @current-change="handleCurrentChange1"
          :cell-style="{ 'text-align': 'center' }"
          border
        >
          <el-table-column
            label="单据号"
            prop="recordCode"
            min-width="150"
            show-overflow-tooltip
          />
          <el-table-column
            label="产品"
            prop="currentProductName"
            min-width="180"
            show-overflow-tooltip
          />
          <el-table-column
            label="领用部门"
            prop="receiveDepartmentName"
            min-width="180"
            show-overflow-tooltip
          />
          <el-table-column
            label="领用人"
            prop="receivePersonName"
            min-width="120"
            show-overflow-tooltip
          />
          <el-table-column
            label="是否换型"
            prop="applyType"
            min-width="80"
            show-overflow-tooltip
            align="center"
          >
            <template #default="scope">
              <el-tag type="info" v-if="scope.row.applyType === 1">否</el-tag>
              <el-tag type="success" v-if="scope.row.applyType === 2"
                >是</el-tag
              >
            </template>
          </el-table-column>
          <el-table-column
            label="申请日期"
            prop="applyDate"
            min-width="150"
            show-overflow-tooltip
          >
            <template #default="scope">
              <div>
                {{ dayjs(scope.row.applyDate).format("YYYY-MM-DD") || "" }}
              </div>
            </template>
          </el-table-column>
          <el-table-column
            label="申请原因"
            prop="applyReason"
            min-width="180"
            show-overflow-tooltip
          />
        </el-table>
      </div>
      <!-- </el-card> -->
      <el-pagination
        :background="true"
        :current-page="pageable.pageNum"
        :page-size="pageable.pageSize"
        :page-sizes="[5, 10, 15, 20]"
        :total="pageable.total"
        layout="total, sizes, prev, pager, next, jumper"
        class="mt-[20px]"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" :loading="loading" @click="handleSubmit"
            >确 定
          </el-button>
          <el-button @click="closeDialog">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup lang="ts">
import { useTable, useDictionary } from "@/utils/useTable";
import { ElMessage, ElForm } from "element-plus";
import { ref, reactive, watch, onBeforeUnmount } from "vue";
import request from "@/utils/request";
import dayjs from "dayjs";
defineOptions({
  name: "EAMCHANGESTATUSSELECT",
  inheritAttrs: false,
});

const emit = defineEmits(["change-visible"]);

const props = defineProps({
  visible: {
    type: Boolean,
    default: () => {
      return false;
    },
  },
});

//获取表格数据 api 方法
const requestApi = "/eam/eammtuseapply/queryByPage";
//获取数据初始化参数
const initParam = {};
// 初始化搜索参数数据
// 初始化搜索参数数据
const searchParamInit = {
  applyType: null,
  receivePersonId: "",
  changeProductId: "",
  applyDateSearch: [],
  applyDateStart: "",
  applyDateEnd: "",
};
// 是否需要分页
const pagination = true;
// 无需二次数据处理&不传赞数也行
const tableData1 = ref([]);
const dataCallBack = (data: any) => {
  // data.forEach((item: any, index: number) => {
  // });
  // if (data) {
  //   data = data.filter(function (value, index, arr) {
  //     return value.assetStatus == "3";
  //   });
  // }
  return data;
};
// const dataCallBack = ref(undefined);
// 数据请求错误后处理
function requestError() {}
//删除
const deleteParam = {
  api: "",
  data: {},
};

const {
  tableData,
  pageable,
  ids,
  searchParam,
  reset,
  search,
  getTableList,
  handleSizeChange,
  handleSelectionChange,
  handleTableDelete,
  handleCurrentChange,
  loading,
}: any = useTable(
  requestApi,
  initParam,
  searchParamInit,
  pagination,
  dataCallBack,
  requestError,
  deleteParam
);

const visibleDialog = ref(false);
watch(
  () => props.visible,
  (newVal: boolean) => {
    visibleDialog.value = newVal;
  }
);

// 选择高亮
const singleTableRef = ref();
const currentRow = ref();
const handleCurrentChange1 = (val: any) => {
  currentRow.value = val;
};

/** 关闭表单弹窗 */
function closeDialog() {
  visibleDialog.value = false;
  emit("change-visible", false);
}

/** email消息发送记录保存提交 */
function handleSubmit() {
  if (currentRow.value) {
    emit("change-visible", false, currentRow.value);
  } else {
    return ElMessage.warning("请选择工作任务");
  }
}

// 获取位置信息下拉树
const locationInfoTree = ref([]);
const getLocationInfoTree = async () => {
  const { data } = await request({
    url: "/eam/locationInfo/getTree",
    method: "POST",
    data: {
      enabled: 1,
    },
  });
  locationInfoTree.value = data;
};

// 目录树
const catalogList: any = ref([]);

// 获取内容树数据
const getCAtalogList = async () => {
  // 获取内容树
  await request({
    url: "eam/mtAssetType/getAvailableTree",
    method: "POST",
    data: {},
  }).then((res) => {
    catalogList.value = res.data;
  });
};

// 获取资产品牌数据
const brandList: any = ref([]);
const getBrandList = async () => {
  await request({
    url: "eam/eamassetbrand/getList",
    method: "post",
    data: {},
  }).then((res) => {
    brandList.value = res.data;
  });
};
// 组件销毁时调用
onBeforeUnmount(() => {
  visibleDialog.value = false;
  emit("change-visible", false);
});

onMounted(() => {
  getTableList();
  // 获取位置信息下拉树
  getLocationInfoTree();
  getCAtalogList();
  getBrandList();

  getProductList();
});

// 获取产品信息下拉树
const productList: any = ref([]);
const getProductList = async () => {
  const { data } = await request({
    url: "/mdm/mdmproductinfo/getList",
    method: "POST",
    data: {
      enabled: 1,
    },
  });
  productList.value = data;
};

// 打开人员选择器方法
const selectPersonData: any = ref([]);
function getPerson(Ids: string[], Objects: any) {
  searchParam.value.receivePersonId = Objects[0]?.id || "";
}

function getapplyDate(data: any) {
  if (data) {
    searchParam.value.applyDateStart = data[0];
    searchParam.value.applyDateEnd = data[1];
  } else {
    searchParam.value.applyDateStart = "";
    searchParam.value.applyDateEnd = "";
  }
}

function resetHandle() {
  selectPersonData.value = [];
  reset();
}
</script>

<style scoped lang="scss">
:deep(.selectDialog .el-dialog__body) {
  padding: 20px;
  // color: black;
  // font-weight: 700;
}
.progress {
  color: red;
}
.success {
  color: rgb(100, 207, 86);
}
</style>
